package servlet;

import entity.Computerroom;
import service.MachineroomService;
import service.impl.MachineroomServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/Add_do")
public class Add_do extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        Computerroom computerroom = new Computerroom();
        computerroom.setRoomName(request.getParameter("roomname"));
        computerroom.setComputerNumber(Integer.parseInt(request.getParameter("amount")));
        computerroom.setManager(request.getParameter("name"));
        computerroom.setLocation(request.getParameter("address"));
        computerroom.setState(Integer.parseInt(request.getParameter("state")));

        response.setContentType("text/html;charset=UTF-8;");
        MachineroomService machineroomService = new MachineroomServiceImpl();
        int count = machineroomService.add(computerroom);
        PrintWriter out = response.getWriter();
        if (count>0){
            out.println(" <script>");
            out.println("       alert('添加成功');");
            out.println("       location.href='Item/ListPage.jsp';");
            out.println("</script>");
        }else {
            out.println(" <script>");
            out.println("       alert('添加失败');");
            out.println("       location.href='Item/AddPage.jsp';");
            out.println("</script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
